define(function (require, exports, module) {
    var myPub = require('./public');
    var oWindow = $(window);
    var giftTell = $('.js_tellOrNot'); 			// 礼品告不告诉
    var giftHour = $('.js_countHour'); 			// 倒计时的小时
    var giftMinute = $('.js_countMinute'); 		// 倒计时的分钟
    var giftSecond = $('.js_countSecond'); 		// 倒计时的秒数
    var oLayerIntro = $('.js_layerIntro'); 		        // 弹出支付说明按钮
    var oLayerIntroText = $('.js_layerIntroText'); 		// 弹出支付说明信息
    var resizeTimer = null;
    module.exports = {
        init: function (remainSecond) {
            shareDetail.giftCountDown(remainSecond,giftHour,giftMinute,giftSecond,function(){});

            oWindow.on('resize', function () {
                if (resizeTimer) {
                    clearTimeout(resizeTimer);
                }
                resizeTimer = setTimeout(function () {

                }, 300)
            })
        }
    }

    oLayerIntro.hammer().on('tap', function () {
        myPub.TipBtnLayer(oLayerIntroText.html(),null,null, function () {
            myPub.LayerCloseAll();
        });
        $('.layermbtn span').css({'width': '100%'});
    })

    var shareDetail = {
        //倒计时
        //totalTime:总秒数   hourObj:小时放置的对象  minuteObj:分钟放置的对象  secondObj:秒数放置的对象  fn:倒计完成的函数
        giftCountDown: function(totalTime, hourObj, minuteObj, secondObj, fn) {
            clearInterval(timer);
            var timer=setInterval(function() {
                var day=0,hour = 0,minute = 0,second = 0; //时间默认值
                if(totalTime > 0) {
                    day = Math.floor(totalTime / (60 * 60 * 24));
                    hour = Math.floor(totalTime / (60 * 60)) - (day * 24);
                    minute = Math.floor(totalTime / 60) - (day * 24 * 60) - (hour * 60);
                    second = Math.floor(totalTime) - (day * 24 * 60 * 60) - (hour * 60 * 60) - (minute * 60);
                }else{
                    clearInterval(timer);
                    secondObj.html('00');
                    if(fn)fn;
                    return;
                }
                if(hour <=9) hour = '0' + hour;
                if(minute <= 9) minute = '0' + minute;
                if(second <= 9) second = '0' + second;

                hourObj.html(hour);
                minuteObj.html( minute);
                secondObj.html(second);
                totalTime--;
            }, 1000);
        }
    }

    $("#submitBtn").on('click',submitFn);

    function submitFn() {
        //验证
        if($.trim($('input[name="userName"]').val()) == ""){
            myPub.TipLayer("请输入收货人姓名");
            return false;
        }
        if( $('.js_userName').parents('.js_userAddrInfo').hasClass('has_error')){
            myPub.TipLayer("请输入正确的姓名");
            return false;
        }
        if($.trim($('input[name="userIphone"]').val()) == ""){
            myPub.TipLayer("请输入正确的手机号码");
            return false;
        }
        if($('input[name="userCity"]').val() == ""){
            myPub.TipLayer("请选择所在地区信息");
            return false;
        }
        /*if($('input[name="userCityStreet"]').val() == ""){
            myPub.TipLayer("请选择所在街区信息");
            return false;
        }*/
        if($.trim($('textarea[name="userAddrDetail"]').val()) == ""){
            myPub.TipLayer("请填写详细地址信息");
            return false;
        }
        var reg = /^(1[3|4|5|7|8])[\d]{9}$/;   //验证手机号码
        if(!reg.test($('input[name="userIphone"]').val()) ){
            myPub.TipLayer("请输入正确的手机号码");
            return false;
        }
        // var encryptStr = '${encryptStr}';
        var encryptStr = $("input[name='encryptStr']").val();
        var userCity = $('input[name="userCity"]').val();
        var array = userCity.split(" ");
        var shipName = $('input[name="userName"]').val();
        var province = array[0];
        var city = array[1];
        var area = array[2];
        var addr =$('input[name="userCityStreet"]').val() + $('textarea[name="userAddrDetail"]').val();
        var shipMobile = $('input[name="userIphone"]').val();
        var paymentOrderId = $("input[name='paymentOrderId']").val();

        $("#submitBtn").off('click');

        $.post('/shop/gift/receive/' + encryptStr,
            {
                shipName: shipName,
                province: province,
                city: city,
                area: area,
                addr: addr,
                shipMobile: shipMobile,
                paymentOrderId: paymentOrderId
            }, function (result) {
                if (result.return_code == "SUCCESS") {
                    // window.location.reload();
                    window.location.href=window.location.href+"?id="+10000*Math.random();
                } else {
                    $("#submitBtn").on('click',submitFn);
                    myPub.TipLayer(result.return_msg);
                }
            }, "json");
    }


})